/**
 * Created by WT on 2017/4/5.
 */
$(function () {
    var PROJECT_SPECIALTY = CONSTANT.DictionaryTypeDefConstants.PROJECT_SPECIALTY;//项目专业类别PROJECT_SPECIALTY
    var PROJECT_OPERATIONSTATE = CONSTANT.DictionaryTypeDefConstants.PROJECT_OPERATIONSTATE;//项目当前状态
    queryComboData(PROJECT_SPECIALTY, 'querySpecialtye');
    queryComboData(PROJECT_OPERATIONSTATE, 'queryOperationState');
    $('#btn_save').on('click', function () {
        submitForm();
    });
    //阶段信息切换事件
    if(projectAccept){
        $('#tt').tabs('select', 2);
    }
    if(projectDesign){
        $('#tt').tabs('select', 1);
    }
    if(projectFSD){
        $('#tt').tabs('select', 0);
    }
    //阶段信息保存按钮
    $('#btn_projectStage_save').on('click', function () {
        var tab = $('#tt').tabs('getSelected');
        var index = $('#tt').tabs('getTabIndex', tab);
        var stageId = $('#projectFSDId').val();
        var state;
        var work;
        switch (index) {
            case 0://可研阶段
                stageId = $('#projectFSDId').val();
                state = 'projectFSD';
                break;
            case 1://基础设计阶段
                stageId = $('#projectDesignId').val();
                state = 'projectDesign';
                break;
            case 2://基础设计阶段
                stageId = $('#projectAcceptId').val();
                state = 'projectAccept';
                break;
        }
        if (stageId != null && stageId != '' && stageId != undefined) {
            work = state + '/edit.shtml';//存在id则进行更新操作
        } else {
            work = state + '/add.shtml';//不存在进行插入
        }
        saveProjectStage(state, work);
    });

    //当前状态为“停止建设”时，停止原因可编辑
    $('#queryOperationState').combobox(
        {
            onSelect: function (record) {
                if (record.text != "停止建设") {
                    $('#stoppageReasons').textbox('setValue', '');
                    $('#stoppageReasons').textbox('textbox').attr("disabled", "disabled").addClass('textbox-disabled');
                } else {
                    $('#stoppageReasons').textbox('textbox').removeAttr("disabled").removeClass('textbox-disabled');
                }
            }
        }
    )

});
//提交表单
function submitForm() {
    var specialNames=$('#querySpecialtye').combobox('getText');
    $('#specialtyNames').val(specialNames);
    //提交基础信息
    $('#projectInfo').form('submit', {
        url: ROOT_PATH + '/threeSimultaneities/projectApproval/edit.shtml',
        onSubmit: function (param) {
            if (!$(this).form('validate')) {
                layer.alert('请正确填写信息', {icon: 5});
            }
            return $(this).form('validate');
        },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.success) {
                layer.msg('保存成功', {
                    icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                }, function () {
                    layer.confirm('是否关闭本页面？', function () {
                        WindowManager.closeLayer({
                            returnValue: true
                        });
                    });
                });
            }
            else {
                layer.msg('保存失败', {
                    icon: 2,
                    time: 1000
                });
            }
        }
    });
}
/**
 * 保存阶段信息
 */
function saveProjectStage(state, work) {
    var formId = '#' + state;
    var url = ROOT_PATH + '/threeSimultaneities/' + work;
    $(formId).form('submit', {
        url: url,
        onSubmit: function (param) {
            if (!$(this).form('validate')) {
                layer.alert('请正确填写信息', {icon: 5});
            }
            return $(this).form('validate');
        },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.success) {
                layer.msg('保存成功', {
                    icon: 1,
                    time: 1000
                }, function () {
                    WindowManager.closeLayer({
                        returnValue: true
                    });
                });
            }
            else {
                layer.msg('保存失败', {
                    icon: 2,
                    time: 1000
                });
            }
        }
    });
}
/**
 * 查询下拉列表
 * @param code
 * @param queryId
 */
function queryComboData(code, queryId) {
    var pageId = '#' + queryId;
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: code},
        dataType: "json",
        success: function (result) {
            var multiple;
            //项目专业可多选
            if(queryId=='querySpecialtye'){
                multiple=true;
            }else{
                multiple=false;
            }
            $(pageId).combobox({
                data: result,
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200',
                multiple:multiple
            });
        }
    });
}